package com.zyd.blog.homeart.service.impl;

import com.zyd.blog.homeart.service.ISoftProductService;
import com.zyd.blog.persistence.beans.*;
import com.zyd.blog.persistence.mapper.CenterMapper;
import com.zyd.blog.persistence.mapper.DictionariesMapper;
import com.zyd.blog.persistence.mapper.MyParticipateMapper;
import com.zyd.blog.persistence.mapper.SoftProductMapper;
import me.zhyd.oauth.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liusongqi
 * @since 2020-02-27
 */
@Service
public class SoftProductServiceImpl  implements ISoftProductService {
    @Autowired
    private SoftProductMapper softProductMapper;
    @Autowired
    private DictionariesMapper dictionariesMapper;
    @Autowired
    private MyParticipateMapper myParticipateMapper;
    @Autowired
    private CenterMapper centerMapper;
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Map<String,Object> soft(String userId,int page,int count) {
        SoftProduct softProduct = new SoftProduct();
        softProduct.setTop("0");
        int i = softProductMapper.selectCount(softProduct);
        List<SoftNew> soft=softProductMapper.soft(userId,count*page-count,count);
        for (SoftNew center : soft) {
            center.setSoftStyle(dictionariesMapper.content(center.getSoftStyle()));
            center.setSoftApartment(dictionariesMapper.content(center.getSoftApartment()));
            center.setSoftPlace(dictionariesMapper.content(center.getSoftPlace()));
        }
        int i1 = softProductMapper.selectCount(null);
        Map<String,Object> map = new HashMap<>();
        map.put("list",soft);
        if (page * count <= i1) {
            map.put("hasMore", 1);
        } else {
            map.put("hasMore", 0);
        }
        return map;
    }


    @Override
    public Map<String, Object> searchDesign(String value, String userId, int page, int count) {
        DesignArticle designArticle = new DesignArticle();
        designArticle.setTop("0");
        int i = softProductMapper.searchCount(value);
        List<SoftNew> soft = softProductMapper.searchDesign(value,userId,  count * page - count, count );
        for (SoftNew center : soft) {
            center.setSoftStyle(dictionariesMapper.content(center.getSoftStyle()));
            center.setSoftApartment(dictionariesMapper.content(center.getSoftApartment()));
            center.setSoftPlace(dictionariesMapper.content(center.getSoftPlace()));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", soft);
        int i1 = softProductMapper.searchCountAll(value);
        //hasmore 1有下一页   0 没有 判断页数和条数  相乘 是否小于 总条数 以此判断 是否有下一页
        if (page * count <= i1) {
            map.put("hasMore", 1);
        } else {
            map.put("hasMore", 0);
        }
        return map;
    }

    @Override
    public   Map<String,Object> sort(String region, String sort, String style, String province, String city, String area, String userId, String place,int page,int count) throws Exception {
        List<SoftNew> sort1;
        try {
            sort1 = softProductMapper.sort(region, sort, style, province, city, area, userId,place,(page - 1) * count, count);
        } catch (Exception e) {
            throw new Exception("获取排序分类失败");
        }
        for (SoftNew center : sort1) {
            center.setSoftStyle(dictionariesMapper.content(center.getSoftStyle()));
            center.setSoftApartment(dictionariesMapper.content(center.getSoftApartment()));
            center.setSoftPlace(dictionariesMapper.content(center.getSoftPlace()));
        }
        SoftProduct softProduct = new SoftProduct();
        int i = softProductMapper.count(region, sort, style, province, city, area, userId,place,(page - 1) * count, count);
        Map<String,Object> map = new HashMap<>();
        map.put("list",sort1);
        if (page * count <= i) {
            map.put("hasMore", 1);
        } else {
            map.put("hasMore", 0);
        }
        return map;
    }

    @Override
    public Integer fabulous(String id, String userId, String category, String state) throws Exception {
            //state 0收藏 1点赞 2评论 3分享
            Integer fabulous = 0;
            MyParticipate myParticipate = new MyParticipate();
            myParticipate.setAddTime(sdf.format(System.currentTimeMillis()));
            myParticipate.setUserId(userId);
            myParticipate.setState(state);
            myParticipate.setArticleId(id);
            myParticipate.setCategory(category);
            myParticipate.setParticipateId(UuidUtils.getUUID());
        if (state.equals("3")){
            Integer fenxaing = myParticipateMapper.fenxaing(id);
            if (fenxaing==0){
                try {
                    myParticipateMapper.insert(myParticipate);
                } catch (Exception e) {
                    throw new Exception("添加到我的参与失败");
                }
            }
        }else{
            try {
                myParticipateMapper.insert(myParticipate);
            } catch (Exception e) {
                throw new Exception("添加到我的参与失败");
            }
            //查询该用户的 该篇文章 是否为第一次 点赞 评论 收藏
            Integer integer = centerMapper.centerCount(id, userId);
            if (integer == 0) {
                try {
                    insert(userId,id,state);
                } catch (Exception e) {
                    throw new Exception("添加center失败");
                }
            }else if (integer == 1){
                try {
                    update(userId,id,state);
                } catch (Exception e) {
                    throw new Exception("修改center失败");
                }
            }
            if (state.equals("0")) {
                try {
                    fabulous = softProductMapper.addCollection(id);
                } catch (Exception e) {
                    throw new Exception("收藏失败");
                }

            } else if (state.equals("1")) {
                try {
                    fabulous = softProductMapper.fabulous(id);
                } catch (Exception e) {
                    throw new Exception("点赞失败");
                }
            }
        }

            return fabulous;
    }

    @Override
    public Integer reduceZan(String id, String userId, String category, String state) throws Exception {
        Integer reduceZan = 0;
        MyParticipate myParticipate = new MyParticipate();
        myParticipate.setState(state);
        myParticipate.setArticleId(id);
        myParticipate.setUserId(userId);
        myParticipate.setCategory(category);
        int delete;
        try {
            delete = myParticipateMapper.delete(myParticipate);
        } catch (Exception e) {
            throw new Exception("我的参与删除失败");
        }

        try {
            cancel(userId, id, state);
        } catch (Exception e) {
            throw new Exception("修改取消 center失败");
        }

        if (state.equals("0")) {
            try {
                reduceZan = softProductMapper.deleteCollection(id);
            } catch (Exception e) {
                throw new Exception("取消收藏失败");
            }
        } else if (state.equals("1")) {
            try {
                reduceZan = softProductMapper.reduceZan(id);
            } catch (Exception e) {
                throw new Exception("取消点赞失败");
            }
        }
        return reduceZan + delete;
    }

    @Override
    public List<SoftProduct> recommendArticle(String designStyle, String designPlace) throws Exception {
        List<SoftProduct> designArticles;
        try {
            designArticles = softProductMapper.recommendArticle(designStyle, designPlace);
        } catch (Exception e) {
            throw new Exception("获取失败");
        }
        return designArticles;
    }

    @Override
    public Map<String, Object> specific(String designId, String userId, String designStyle, String designPlace) throws Exception {
        Map<String,Object> map = new HashMap<>();
        try {
            SoftNew specific = softProductMapper.specific(designId, userId);
            specific.setSoftStyle(dictionariesMapper.content(specific.getSoftStyle()));
            specific.setSoftApartment(dictionariesMapper.content(specific.getSoftApartment()));
            specific.setSoftPlace(dictionariesMapper.content(specific.getSoftPlace()));
            String[] split = specific.getImageId().split(",,");
            String[] iamgeSplit = specific.getSoftImage().split(",,");
            List<Map> list = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                Map<String,Object> haMap = new HashMap<>();
                List<ProductNew> product = softProductMapper.product(split[i]);
                haMap.put("count",product.size());
                haMap.put("image",iamgeSplit[i]);
                haMap.put("product",product);
                haMap.put("content",product.get(0).getContent());
                list.add(haMap);
            }
            map.put("specific",specific);
            map.put("list",list);

        } catch (Exception e) {
            throw new Exception("获取内容失败");
        }
        List<SoftProduct> designArticles;
        try {
            designArticles = softProductMapper.recommendArticle(designStyle, designPlace);
            map.put("softArticles",designArticles);
        } catch (Exception e) {
            throw new Exception("获取失败");
        }

        return map;
    }

    @Override
    public Object myShare(String userId, String state, String category,int page,int count) throws Exception {
    //0收藏 1点赞 2评论 3分享
//        try {
            switch (category) {
                case "1":
                    Integer integer = myParticipateMapper.JZnum(state, userId);
                    Map<String,Object> map = new HashMap<>();
                        if (state.equals("2")){
                            List<CenterNewComment> centerNews = myParticipateMapper.relationJZComment(userId, state, page * count - count, count);
                            for (CenterNewComment centerNew : centerNews) {
                                centerNew.setDesignStyle(dictionariesMapper.content(centerNew.getDesignStyle()));
                                centerNew.setApartmentContent(dictionariesMapper.content(centerNew.getApartmentContent()));
                                centerNew.setDesignPlace(dictionariesMapper.content(centerNew.getDesignPlace()));
                            }
                            map.put("list",centerNews);
                        }else {
                            List<CenterNew> centerNews = myParticipateMapper.relationJZ(userId, state, page * count - count, count);
                            for (CenterNew centerNew : centerNews) {
                                centerNew.setDesignStyle(dictionariesMapper.content(centerNew.getDesignStyle()));
                                centerNew.setApartmentContent(dictionariesMapper.content(centerNew.getApartmentContent()));
                                centerNew.setDesignPlace(dictionariesMapper.content(centerNew.getDesignPlace()));
                            }
                            map.put("list",centerNews);
                        }

                    if (page * count <= integer) {
                        map.put("hasMore", 1);
                    } else {
                        map.put("hasMore", 0);
                    }
                    return map;
                case "2":
                    Integer integer1 = myParticipateMapper.RZnum(state, userId);
                    Map<String,Object> map1 = new HashMap<>();
                    if (state.equals("2")){
                        List<SoftNewComment> softNews = myParticipateMapper.relationRZComment(userId, state, page * count - count, count);
                        for (SoftNewComment center : softNews) {
                            center.setSoftStyle(dictionariesMapper.content(center.getSoftStyle()));
                            center.setSoftApartment(dictionariesMapper.content(center.getSoftApartment()));
                            center.setSoftPlace(dictionariesMapper.content(center.getSoftPlace()));
                        }
                        map1.put("list",softNews);
                    }else {
                        List<SoftNew> softNews = myParticipateMapper.relationRZ(userId, state, page * count - count, count);
                        for (SoftNew center : softNews) {
                            center.setSoftStyle(dictionariesMapper.content(center.getSoftStyle()));
                            center.setSoftApartment(dictionariesMapper.content(center.getSoftApartment()));
                            center.setSoftPlace(dictionariesMapper.content(center.getSoftPlace()));
                        }
                        map1.put("list",softNews);
                    }
                    if (page * count <= integer1) {
                        map1.put("hasMore", 1);
                    } else {
                        map1.put("hasMore", 0);
                    }
                    return map1;
                case "3":
                    Integer integer2 = myParticipateMapper.JPnum(state, userId);
                    Map<String,Object> map2 = new HashMap<>();
                    if (state.equals("2")){
                        List<FineArtNewComment> fineArtNews = myParticipateMapper.relationYSComment(userId, state, page * count - count, count);
                        map2.put("list",fineArtNews);
                    }else {
                        List<FineArtNew> fineArtNews = myParticipateMapper.relationYS(userId, state, page * count - count, count);
                        map2.put("list",fineArtNews);
                    }
                    if (page * count <= integer2) {
                        map2.put("hasMore", 1);
                    } else {
                        map2.put("hasMore", 0);
                    }
                    return map2;

            }
//        } catch (Exception e) {
//            throw new Exception("获取失败");
//        }
        return null;
    }

    //0收藏 1点赞 2评论 3分享 b
    private void insert(String userId, String id, String state) {
        Center center = new Center();
        center.setArticleId(id);
        center.setUserId(userId);
        switch (state) {
            case "0":
                center.setCollection("0");
                center.setZan("1");
                center.setDiscuss("1");
                break;
            case "1":
                center.setZan("0");
                center.setDiscuss("1");
                center.setCollection("1");
                break;
            case "2":
                center.setDiscuss("0");
                center.setZan("1");
                center.setCollection("1");
                break;
        }

        centerMapper.insert(center);
    }
    //0收藏 1点赞 2评论 3分享
    private void update(String userId, String id, String state) {
        centerMapper.update(userId, id, state);
    }
    //  取消  0收藏 1点赞 2评论 3分享
    private void cancel(String userId, String id, String state){
        centerMapper.cancel(userId, id, state);
    }
}
